﻿





namespace Orid.Management.SOHO.Book.Client.UI
{
    using System;
    using System.Windows.Forms;
    using Orid.Management.SOHO.Book.Control.MPK_Calendar;
    using Orid.Management.SOHO.Book.Client.Logic;
    using System.Collections.Generic;
    using Orid.Management.SOHO.Book.Service;

    public partial class FormBook : Form
    {
 
        /// <summary>
        /// Initializes a new instance of the <see cref="FormBook"/> class.
        /// </summary>
        public FormBook()
        {
            InitializeComponent();

            utilBook =  //new UtilBook(@"http://192.168.161.160/WCF_Release/ServiceBook.svc");
                 new UtilBook(@"http://localhost:1154/ServiceBook.svc");
            loginName = "sydy0000";

          // utilBook.UpdateSchedule(loginName);
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="client">ex:  @"http://192.168.161.177/booksystem_wcf/ServiceBook.svc"</param>
        /// <param name="loginName">ex: "sydy01"</param>
        /// <example>  new FormBook(@"http://192.168.161.177/booksystem_wcf/ServiceBook.svc","sydy01")  </example>
        public FormBook( Service.Client  client,string loginName)
        {
            InitializeComponent();

            this.utilBook = new UtilBook( client)  ;
            this.loginName = loginName;
        }

        /// <summary>
        /// 构造函数
        /// soho 使用
        /// </summary>
        /// <param name="bookServerURI">ex:  @"http://192.168.161.177/booksystem_wcf/ServiceBook.svc"</param>
        /// <param name="loginName">ex: "sydy01"</param>
        /// <example>  new FormBook(@"http://192.168.161.177/booksystem_wcf/ServiceBook.svc","sydy01")  </example>
        public FormBook(string bookServerURI, string loginName)
        {
            InitializeComponent();

            this.utilBook = new UtilBook(bookServerURI);
            this.loginName = loginName;
        }

        #region 私有函数

        private UtilBook utilBook;
        private string loginName;

        /// <summary>
        /// Gets the present nextwork schedule Message.
        /// </summary>
        /// <value>The present nextwork schedule Message.</value>
        private string PresentNextworkSchedule
        {
            get
            {
                BookRecord bookRecord = utilBook.GetNextWorkTime(loginName);

                if (bookRecord !=null)
                {
                    return string.Format("次回の勤務開始予定日時は、{0}―{1}です。", 
                        bookRecord.BookDateFrom.ToString("yyyy-MM-dd HH:mm:ss"),
                        bookRecord.BookDateTo.ToString("HH:mm:ss"));
                }
                else
                {

                    return "次回の勤務開始予定日時は、無いです。";
                }
              
            }
        }

        private void FormBook_Load(object sender, EventArgs e)
        {
           mpK_Calendar1.SelectedDateChanged +=new SelectedDateChangedEventHandler(mpK_Calendar1_SelectedDateChanged);
           mpK_Calendar1.SelectedAppointmentDateChanged += new SelectedAppointmentDateChangedEventHandler(mpK_Calendar1_SelectedAppointmentDateChanged);


            DateTime curentTime = utilBook.GetBookTime();

            mpK_Calendar1.SelectedDate = curentTime==DateTime.MinValue? System.DateTime.Now:curentTime;
            mpK_Calendar1.ShowPopMeun =true; 

      
            //设定期限
           // SetLimitEnableDate(mpK_Calendar1.SelectedDate.Date, mpK_Calendar1.SelectedDate.Date.AddDays(30));
           // SetLimitEnableDate(DateTime.MinValue, DateTime.MinValue);

            //DateTime? date = utilBook.GetNextWorkTime( loginName);


            initTimesPicker();
            ShowPlain();
           
        }

        /// <summary>
        /// Shows the plain.
        /// </summary>
        private void ShowPlain()
        {
            Book.Service.BookRecord[] bookRecords = utilBook.GetBookList(loginName,
                                 mpK_Calendar1.SelectedDate.AddMonths(-1),
                                 mpK_Calendar1.SelectedDate.AddMonths(1));



            if (bookRecords != null)
            { 
                List<BookTime> bookTimes = new List<BookTime>();
                foreach (BookRecord bookRecord in bookRecords)
                {
                    bookTimes.Add(new BookTime() {From = bookRecord.BookDateFrom, To = bookRecord.BookDateTo});

                }
                  SetPlan(bookTimes);

                ////如果当天有预约时间
                ////时间段选择的控件上显示预约时间
                List<BookTime> bookRecordsCurrentDays =
                    bookTimes.FindAll(t => t.From.Date.CompareTo(this.mpK_Calendar1.SelectedDate.Date) == 0);

                if (bookRecordsCurrentDays .Count>0 )
                    timeslotpicker1.SetAppointmentTime(bookRecordsCurrentDays.ToArray());

               
            }

 
        }

        /// <summary>
        /// Inits the times picker.
        /// </summary>
        private void initTimesPicker()
        {
             
            BookSetting[] bookSettings = utilBook.GetCurrentBookSetting();

            if (bookSettings.Length == 5)
            {

                foreach (BookSetting bookSetting in bookSettings)
                {
                    BookPicker.SetBookTime(
                        bookSetting.Seqno,
                        new BookTime()
                        {
                            From = bookSetting.BookFrom,
                            To = bookSetting.BookTo
                        }
                        );

                }
            }
            
            timeslotpicker1.SetTimeslot(utilBook.GetBookTime());
             
        }

        /// <summary>
        /// 选中了预订日期响应的事件
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Orid.Management.SOHO.Book.Control.MPK_Calendar.SelectedAppointmentDateChangeEventArgs"/> instance containing the event data.</param>
        private void mpK_Calendar1_SelectedAppointmentDateChanged(object sender, SelectedAppointmentDateChangeEventArgs e)
        {
            //MessageBox.Show("预订" + e.ApptDate.Desc, "选中了预约日期");
            List<BookTime> bookTimes = new List<BookTime>();

            List<clsAppt> ApptDates = e.ApptDates;

            if (ApptDates != null)
            {
            
                foreach (clsAppt apptDate in ApptDates)
                {
                    bookTimes.Add(new BookTime() {From = apptDate.ApptStart, To = apptDate.ApptEnd});
                }
             }


            timeslotpicker1.SetAppointmentTime(bookTimes.ToArray());
        }

        /// <summary>
        /// 选中了普通日期响应的事件
        /// timeslotpicker 跟随响应
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mpK_Calendar1_SelectedDateChanged(object sender, SelectedDateChangedEventArgs e)
        {
            SetTimeslotPickerCurrentDays(e.SelectedDate);
        }

        /// <summary>
        /// Sets the timeslot picker current days.
        /// </summary>
        /// <param name="selectDate">The select date.</param>
        private void SetTimeslotPickerCurrentDays(DateTime selectDate)
        {
            DateTime currentDate = utilBook.GetBookTime();
            if (currentDate.Date.CompareTo(selectDate) == 0 )
            {
                selectDate = currentDate;
            }

            timeslotpicker1.SetTimeslot(selectDate);

            //增加翻页刷新
            ShowPlain();
        }

        /// <summary>
        /// 预约按钮响应的单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Appointment_Click(object sender, EventArgs e)
        {
            string msg  =string.Empty;

          
               // SetPlan(timeslotpicker1.ScheduleData);
                RemovePlan(timeslotpicker1.UnScheduleData);
                SavaPlan(timeslotpicker1.ScheduleData);
              //  msg += bookTime.From.ToString() + Environment.NewLine;

            ShowPlain();

            // MessageBox.Show(msg);
        }

        /// <summary>
        /// Removes the plan.
        /// </summary>
        /// <param name="bookTimes">The book times.</param>
        private void RemovePlan(List<BookTime> bookTimes)
        {
            if (bookTimes != null)
            {


                foreach (BookTime bookTime in bookTimes)
                {

                    utilBook.RemoveSchedule(loginName, bookTime.From, bookTime.To);
                }


            }
        }

        /// <summary>
        /// 保存预约数据
        /// </summary>
        private void SavaPlan(List<BookTime> bookTimes)
        {

            if (bookTimes != null)
            {
                

                foreach (BookTime bookTime in bookTimes)
                {

                     utilBook.AddSchedule(loginName, bookTime.From, bookTime.To);
                }

                
            }

             
             MessageBox.Show(PresentNextworkSchedule, @"Message");
            
        }

        /// <summary>
        /// mpK_Calendar1中将日期预约设置成红色
        /// </summary>
        /// <param name="bookTimes">The book times.</param>
        private void SetPlan(List<BookTime> bookTimes)
        {
            

            if (bookTimes != null)
            {
                //清空以前的预订 否则重复
                mpK_Calendar1.Appointments.Clear();

                foreach (BookTime bookTime in bookTimes)
                {
                    
                        
                    EBookPciker eBookPicker =    BookPicker.GetBookPicker(new BookTime[] {bookTime}) ;

                    string PresentNextworkSchedule = eBookPicker == EBookPciker.Empty
                                                         ? string.Empty
                                                         : eBookPicker.ToString();

                    clsAppt appt = new clsAppt(bookTime.From, bookTime.To, PresentNextworkSchedule);
                    mpK_Calendar1.Appointments.Add(appt);
                }
     
                    mpK_Calendar1.Refresh();
            }

       
        }



        #endregion

        /// <summary>
        /// 设定日历的限制使用日期
        /// 如果没有限定日期，不填写
        /// </summary>
        /// <param name="from">限制的开始日期，只要日期部分;如果不需要限制，set param = DateTime.MinValue</param>
        /// <param name="to">限制的结束日期，只要日期部分;如果不需要限制，set param = DateTime.MinValue</param>
        public void SetLimitEnableDate(DateTime from, DateTime to)
        {

            this.mpK_Calendar1.LimitEnableDateFrom = this.timeslotpicker1.LimitEnableDateFrom = from;
            this.mpK_Calendar1.LimitEnableDateTo = this.timeslotpicker1.LimitEnableDateTo = to;
            this.mpK_Calendar1.Refresh();
        }

        /// <summary>
        /// Handles the Click event of the buttonClose control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void buttonClose_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
